# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np

data = pd.read_csv("../notebook/day.csv")

data =  data.drop('dteday', axis=1) \
    .drop('season', axis=1) \
    .drop('mnth', axis=1) \
    .drop('weekday', axis=1) \
    .drop('registered', axis=1) \
    .drop('hum', axis=1) \
    .drop('casual', axis=1)

print(data.head(2))

# data_splits = np.split(data, [4] , axis=1)
# print(data_splits[0].head(1))
# print(data_splits[1].head(1))

import sklearn.preprocessing as preprocessing
min_max_scaler = preprocessing.MinMaxScaler()

# data["instant"] =  min_max_scaler.fit_transform(data["instant"])
data["holiday"] =  min_max_scaler.fit_transform(data["holiday"])
data["workingday"] =  min_max_scaler.fit_transform(data["workingday"])
data["weathersit"] =  min_max_scaler.fit_transform(data["weathersit"])

print(" >>> min max scaler ")
print(data.head(1))

data_0 = data[data.yr == 0]
data_1 = data[data.yr == 1]
y_train = data_0['cnt'].values
y_test = data_1['cnt'].values
X_train = data_0.drop('cnt', axis = 1).drop('yr', axis = 1)
X_test = data_1.drop('cnt', axis = 1).drop('yr', axis = 1)

print(" >>> X_train ")
print(X_train.head())
print(y_train[0])
# print(X_test_raw.head(1))

print("========= 岭回归 =============")
from sklearn.linear_model import  RidgeCV
from sklearn.metrics import r2_score

#设置超参数（正则参数）范围
alphas = [0.01, 0.1, 1.0, 10 ,100, 200 ]
#n_alphas = 20
#alphas = np.logspace(-5,2,n_alphas)

#生成一个RidgeCV实例
ridge = RidgeCV(alphas=alphas, store_cv_values=True)
#模型训练
ridge.fit(X_train, y_train)
#预测
y_test_pred_ridge = ridge.predict(X_test)
y_train_pred_ridge = ridge.predict(X_train)

print ('alpha is:', ridge.alpha_)
# 评估，使用r2_score评价模型在测试集和训练集上的性能
print 'The r2 score of RidgeCV on test is', r2_score(y_test, y_test_pred_ridge)
print 'The r2 score of RidgeCV on train is', r2_score(y_train, y_train_pred_ridge)

print("========= 拉锁回归 =============")
from sklearn.linear_model import LassoCV

#设置超参数搜索范围
alphas = [ 0.01, 0.1, 1, 10]

#生成一个LassoCV实例
lasso = LassoCV(alphas = alphas)

#训练（内含CV）
lasso.fit(X_train, y_train)

#测试
y_test_pred_lasso = lasso.predict(X_test)
y_train_pred_lasso = lasso.predict(X_train)

print ('alpha is:', lasso.alpha_)
# 评估，使用r2_score评价模型在测试集和训练集上的性能
print 'The r2 score of LassoCV on test is', r2_score(y_test, y_test_pred_lasso)
print 'The r2 score of LassoCV on train is', r2_score(y_train, y_train_pred_lasso)